from config import SIGNAL_PATHS

class SignalController:
    def __init__(self, canvas, text_manager, section_manager):
        self.canvas = canvas
        self.text_manager = text_manager
        self.section_manager = section_manager
        self.signals = {}
        self._create_signals()
        self.selected_signal = None  # 记录第一次点击的信号机
    
    def _create_signals(self):
        """创建信号灯"""
        # 创建信号灯
        self.canvas.create_line(370, 110, 370, 130, width=2)
        self.canvas.create_line(370, 120, 360, 120, width=2)
        self.signals['F4'] = self.canvas.create_oval(340, 110, 360, 130, fill="red")
        
        self.canvas.create_line(900, 110, 900, 130, width=2)
        self.canvas.create_line(900, 120, 890, 120, width=2)
        self.signals['XC'] = self.canvas.create_oval(870, 110, 890, 130, fill="red")
        
        self.canvas.create_line(1450, 110, 1450, 130, width=2)
        self.canvas.create_line(1450, 120, 1440, 120, width=2)
        self.signals['F8'] = self.canvas.create_oval(1420, 110, 1440, 130, fill="red")
        
        self.canvas.create_line(350, 260, 350, 280, width=2)
        self.canvas.create_line(350, 270, 340, 270, width=2)
        self.signals['F2'] = self.canvas.create_oval(320, 260, 340, 280, fill="red")
        
        self.canvas.create_line(480, 320, 480, 340, width=2)
        self.canvas.create_line(480, 330, 490, 330, width=2)
        self.signals['F5'] = self.canvas.create_oval(510, 320, 490, 340, fill="red")
        
        self.canvas.create_line(820, 410, 820, 430, width=2)
        self.canvas.create_line(820, 420, 810, 420, width=2)
        self.signals['F6'] = self.canvas.create_oval(790, 410, 810, 430, fill="red")
        
        self.canvas.create_line(120, 490, 120, 470, width=2)
        self.canvas.create_line(120, 480, 130, 480, width=2)
        self.signals['F1'] = self.canvas.create_oval(150, 490, 130, 470, fill="red")
        
        self.canvas.create_line(490, 490, 490, 470, width=2)
        self.canvas.create_line(490, 480, 500, 480, width=2)
        self.signals['F3'] = self.canvas.create_oval(520, 490, 500, 470, fill="red")
        
        self.canvas.create_line(1100, 490, 1100, 470, width=2)
        self.canvas.create_line(1100, 480, 1110, 480, width=2)
        self.signals['SC'] = self.canvas.create_oval(1130, 490, 1110, 470, fill="red")
    
    def set_signal_color(self, signal_name, color):
        """设置信号灯颜色"""
        if signal_name in self.signals:
            self.canvas.itemconfig(self.signals[signal_name], fill=color)
    
    def get_signal_color(self, signal_name):
        """获取信号灯颜色"""
        if signal_name in self.signals:
            return self.canvas.itemcget(self.signals[signal_name], "fill")
        return None

    def draw_route(self, path):
        # 清除之前的绿色进路
        self.canvas.delete("route_green")
        for section in path:
            section_info = self.section_manager.sections[section]
            coords = section_info['coords']
            self.canvas.create_line(
                coords[0], coords[1], coords[2], coords[3],
                width=5, fill="green", tags="route_green"
            )

    def on_signal_click(self, signal_name):
        if self.selected_signal is None:
            self.selected_signal = signal_name
            self.text_manager.insert_text_with_delay([f"已选择起点信号机: {signal_name}，请再选择终点信号机"])
        else:
            start = self.selected_signal
            end = signal_name
            self.selected_signal = None
            path = SIGNAL_PATHS.get((start, end)) or SIGNAL_PATHS.get((end, start))
            if not path:
                self.text_manager.insert_text_with_delay([f"{start}与{end}之间无通路"])
                return
            # 检查区段占用
            for section in path:
                if self.section_manager.is_section_occupied(section):
                    self.text_manager.insert_text_with_delay([f"区段{section}被占用，无法建立进路"])
                    return
            self.text_manager.insert_text_with_delay([f"允许建立{start}→{end}进路！"])
            self.draw_route(path)  # 自动绘制绿色进路 